package com.twxm.breed.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.twxm.breed.entity.*;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
public class List2JSONArray {
    public static JSONArray waterList2JsonArray(List<WaterFactor> list, String pondId, String factor){
        JSONArray jsonArray = new JSONArray();
        for (WaterFactor waterFactor:list){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("waterTemperature",waterFactor.getWaterTemperature());
            jsonObject.put("PH",waterFactor.getPH());
            jsonObject.put("ammoniaNitrogen",waterFactor.getAmmoniaNitrogen());
            jsonObject.put("totalNitrogen",waterFactor.getTotalNitrogen());
            jsonObject.put("totalPhosphorus",waterFactor.getTotalPhosphorus());
            jsonObject.put("permanganate",waterFactor.getPermanganate());
            jsonObject.put("nitrite",waterFactor.getNitrite());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String reportDateStr = formatter.format(waterFactor.getCollectionTime());
            jsonObject.put("time", reportDateStr);
            jsonObject.put("pondid",pondId);
            jsonObject.put("factor",factor);
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    public static JSONArray weatherList2JsonArray(List<WeatherFactor> list, String pondId,String factor){
        JSONArray jsonArray = new JSONArray();
        for (WeatherFactor weatherFactor:list){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("temperature",weatherFactor.getTemperature());
            jsonObject.put("windSpeed",weatherFactor.getWindSpeed());
            jsonObject.put("airPressure",weatherFactor.getAirPressure());
            jsonObject.put("illumination",weatherFactor.getIllumination());
            jsonObject.put("humidity",weatherFactor.getHumidity());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String reportDateStr = formatter.format(weatherFactor.getCollectionTime());
            jsonObject.put("time", reportDateStr);
            jsonObject.put("pondid",pondId);
            jsonObject.put("factor",factor);
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    public static JSONArray oxygenList2JsonArray(List<DissolvedOxygen> list, String pondId, String factor){
        JSONArray jsonArray = new JSONArray();
        for (DissolvedOxygen dissolvedOxygen:list){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("oxygenContent",dissolvedOxygen.getOxygenContent());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String reportDateStr = formatter.format(dissolvedOxygen.getCollectionTime());
            jsonObject.put("time", reportDateStr);
            jsonObject.put("pondid",pondId);
            jsonObject.put("factor",factor);
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    //JSONArray{JsonObject[xxx:]}
    //{
    //  "place1": [
    //    [time1, value1],
    //    [time2, value2],
    //    ...
    //  ],
    //  "place2": []
    //}
    public static JSONObject elList2Json(List<FactorImpl> list, List<String> pondIds, String el){
        JSONObject jsonObject = new JSONObject();
        for (String pondId : pondIds){
            jsonObject.put(pondId,new JSONArray());
        }
        for (FactorImpl factor:list){
            FishPond fishPond = factor.getFishPond();
            String pondid = fishPond.getPondId();
            JSONArray jsonArray = (JSONArray) jsonObject.get(pondid);
            JSONArray jsonArray1 = new JSONArray();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String reportDateStr = formatter.format(factor.getCollectionTime());
            jsonArray1.add(reportDateStr);
            jsonArray1.add(factor.getContent(el));
            jsonArray.add(jsonArray1);
        }
        return jsonObject;
    }

    //1.根据list和pondid返回一个JsonObject
    //2.根据
}
